package com.sky.service.impl;

import com.sky.constant.CommentConstant;
import com.sky.constant.DefaultImageConstant;
import com.sky.dto.ReplyDTO;
import com.sky.entity.User;
import com.sky.event.ReplyCreatedEvent;
import com.sky.exception.CommentSendException;
import com.sky.mapper.ReplyMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReplyService;
import com.sky.vo.ReplyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReplyServiceImpl implements ReplyService {

    private final RedisTemplate<String ,String> redisTemplate;

    private final UserMapper userMapper;

    private final ReplyMapper replyMapper;

    private ApplicationEventPublisher eventPublisher;

    @Autowired
    public ReplyServiceImpl(ReplyMapper replyMapper,
                            UserMapper userMapper,
                            RedisTemplate<String, String> redisTemplate,
                            ApplicationEventPublisher eventPublisher) {
        this.replyMapper = replyMapper;
        this.userMapper = userMapper;
        this.redisTemplate = redisTemplate;
        this.eventPublisher = eventPublisher;
    }

    /**
     * 添加回复
     *
     * @param replyDTO 回复信息
     */
    @Override
    public void addReply(ReplyDTO replyDTO) {

        //根据昵称查询user是否存在
        User user = userMapper.selectByUsername(replyDTO.getNickname());


        //如果不存在则新增user
        if(user == null){

            user = User.builder()
                    .nickname(replyDTO.getNickname())
                    .email(replyDTO.getEmail())
                    .avatar(DefaultImageConstant.MAN)
                    .build();
            userMapper.insertUser(user);
        }

        //验证邮箱验证码
        //获取验证码
        String code = redisTemplate.opsForValue().get(replyDTO.getEmail());
        if(code == null){
            //验证码过期
            throw new CommentSendException(CommentConstant.CODE_EXPIRE);
        }
        if(!code.equals(replyDTO.getCode())){
            //验证码错误
            throw new CommentSendException(CommentConstant.CODE_ERROR);
        }

        //新增回复，并主键回写
        replyMapper.addReply(replyDTO);

        //获取新增的回复id
        Long replyId = replyDTO.getId();

        // 只有回复别人才发邮件，replyToId != null 且被回复人有邮箱
        if (replyDTO.getReplyToId() != null) {
            // 查被回复人邮箱
            User target = userMapper.selectByNickname(replyDTO.getReplyToNickname());

            if (target != null && target.getEmail() != null) {

                //发布事件
                eventPublisher.publishEvent(new ReplyCreatedEvent(
                        replyId,
                        replyDTO.getNickname(),
                        replyDTO.getContent(),
                        target.getEmail()));
            }
        }
    }

    /**
     * 根据评论id查询回复列表
     *
     * @param commentId 评论id
     * @return 回复列表
     */
    @Override
    public List<ReplyVO> listReply(Long commentId) {
        return replyMapper.listReply(commentId);
    }
}
